package jt.classic.app.info;

import jt.classic.system.user.IUser;

import org.limp.mine.DateTrimmer;
import org.limp.mine.HTMLTool;
import org.limp.mine.StringTool;

import java.util.List;
import java.util.Map;


public class InfoTool {
    /**
     * 是否有权限阅读信息
     * @param info
     * @param user
     * @return boolean
     */
    public static boolean canread(AInfo info, IUser user) {
        boolean rnt = false;

        AInfoIssue issue = info.issue();

        String groupid = user.iallgroupflumeid(",") + ",";

        if (issue != null) {
            String[] idstrs = issue.iareaid().split(",");

            for (int i = 0; i < idstrs.length; i++) {
                if (!idstrs[i].equals("") &&
                        (groupid.indexOf(idstrs[i] + ",") > -1)) {
                    rnt = true;

                    break;
                }
            }
        }

        return rnt;
    }

    /**
     * List 结果集
     * <p>
     *   <div>List结构-->{[Map],[Map]}</div>
     *   <div>
     *        stamp结构-->id:PKID;name:PKNAME;alt:PKMEMO;
     *        other:OTHER;title:数据来源;type:radio(或者checkbox);
     *        cid:sjly;size:5
     *   </div>
     *   <div>return Table HTML</div>
     *   <div>table class is 'intable'</div>
     *   <div>tr class is 'intr'</div>
     *   <div>td class is 'intd'</div>
     *   <div>checkbox/radio onclick is 'handleThizz'</div>
     *   <div>checkbox/radio id is cid + "_chk_" + j</div>
     * </p>
     * @param list 结果集
     * @param stamp 特征值
     * @return String 结果集
     */
    public static String rangeThemeList(List list, String stamp, String context) {
        StringBuffer temp = new StringBuffer();

        String[] splitstr = StringTool.checkString(stamp).split(";");
        String kid = "";
        String kname = "";
        String kmemo = "";
        String kother = "";
        String title = "";
        String type = "";
        String cid = "";
        int size = 5;

        for (int i = 0; i < splitstr.length; i++) {
            String str = StringTool.checkString(splitstr[i]);

            if (!str.equals("")) {
                if ((str.lastIndexOf(":") != -1) &&
                        (str.lastIndexOf(":") < str.length())) {
                    /*for id*/
                    if (str.substring(0, str.lastIndexOf(":")).equals("id")) {
                        kid = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for name*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("name")) {
                        kname = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for alt*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("alt")) {
                        kmemo = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for other*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("other")) {
                        kother = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for title*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("title")) {
                        title = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for type*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("type")) {
                        type = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for cid*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("cid")) {
                        cid = str.substring(str.lastIndexOf(":") + 1,
                                str.length());
                    }
                    /*for size*/
                    else if (str.substring(0, str.lastIndexOf(":")).equals("size")) {
                        try {
                            size = Integer.parseInt(str.substring(str.lastIndexOf(
                                            ":") + 1, str.length()));
                        } catch (Exception ex) {
                            System.err.println(
                                "Parameter 'size' must be Integer.");
                        }

                        if (size <= 0) {
                            size = 5;
                        }
                    }
                }
            }
        }

        if ((list != null) && !list.isEmpty()) {
            temp.append("<table class=intable title=" + title + ">");

            int count = ((list.size() % size) > 0) ? ((list.size() / size) + 1)
                                                   : (list.size() / size);

            for (int i = 0; i < count; i++) {
                temp.append("<tr class=intr>");

                for (int j = i * size; j < ((i * size) + size); j++) {
                    if (j < list.size()) {
                        Object[] obj = (Object[]) list.get(j);
                        String number = (String) obj[0];
                        AInfoDirectory direc = (AInfoDirectory) obj[1];
                        String purview = (String) obj[2];
                        Map info = direc.getResource();
                        String labelimg = (String) info.get("ID_LABELIMGURL");
                        String name = HTMLTool.htmlEscape(direc.iname());
                        String memo = HTMLTool.htmlEscape(direc.imemo());
                        String date = direc.idate();
                        String username = direc.iusername();
                        String id = direc.id();

                        String url = context +
                            "/app/info.do?method=face2directory&id=" + id;
                        
                        /*String url1 = context +
                        "/app/info.do?method=editdirectory&id=" + id;*/
                        
                        String url1 = context + "/app/info/directorymap.jsp?id=" + id;

                        temp.append("<td class=intd valign=top title='" + memo +
                            "'>");
                        temp.append("<div style=\"padding:5px;\" align=\"center\">");

                        temp.append("<div style=\"padding:5px;\" align=\"center\"><a href=\"" + url +
                            "\" target=\"_blank\"><img class=\"inimg\" onmouseover=\"this.className='inimgover'\" " +
                            "onmouseout=\"this.className='inimgout'\" src=\"" +
                            labelimg + "\" alt=\"点击浏览专题《" + direc.iname() +
                            "》\"></a></div>");
                        temp.append("<div style=\"padding:5px;\">");
                        temp.append("<div align=\"center\"><a href=\"" + url +
                            "\" target=\"_blank\" title=\"点击浏览专题《" +
                            direc.iname() + "》\">" + name + "</a></div>");

                        temp.append("<div align=\"center\">" +
                            DateTrimmer.getYMD_LBL(date) + "</div>");

                        if (purview.equals("2")) {
                            temp.append(
                                "<div style=\"padding:5px;\" align=\"center\"><a href=\""+url1+"\" target=\"target\">专题/栏目管理</a></div>");
                        }

                        temp.append("</div>");

                        temp.append("</div>");
                        temp.append("</td>");
                    } else {
                        temp.append("<td class=intd>");
                        temp.append("<div style=\"padding:5px;width:100px;\">&nbsp;</div>");
                        temp.append("</td>");
                    }
                }

                temp.append("</tr>");
            }

            temp.append("</table>");
        }

        return temp.toString();
    }
}
